﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Xceed.Wpf.Toolkit.PropertyGrid;
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;

namespace ABCat.Core.Plugins
{
	public class EnumEditor : ITypeEditor
	{
		#region + Logic +

		public FrameworkElement ResolveEditor(PropertyItem propertyItem)
		{
			var fcmb = new ComboBox();
			var binding = new Binding("Value");

			binding.Source = propertyItem;
			binding.ValidatesOnExceptions = true;
			binding.ValidatesOnDataErrors = true;
			binding.Converter = new LocalizationValueConverter() { TargetType = propertyItem.PropertyType };
			binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
			BindingOperations.SetBinding(fcmb, ComboBox.SelectedValueProperty, binding);
			fcmb.ItemsSource = Enum.GetValues(propertyItem.PropertyType).Cast<Enum>().Select(item => item.GetDescription());
			return fcmb;
		}

		#endregion
	}
}